package cc.angis.xiugallery.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "jbxc.db";
	private static final int DATABASE_VERSION = 1;

	private static final String SQL_FAVOURITE = "CREATE TABLE " + TableConstants.TABLE_FAVOURITE + "(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name TEXT NOT NULL, lastupdate LONG NOT NULL, lastimgid LONG, imgcount INTEGER)";
	private static final String SQL_FAVOURITE_IMAGES = "CREATE TABLE " + TableConstants.TABLE_FAVOURITE_IMAGES + "(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, favid INTEGER NOT NULL, imgid LONG NOT NULL)";
	private static final String SQL_IMAGE_DESC = "CREATE TABLE " + TableConstants.TABLE_IMAGE_DESC + "(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, imgid LONG NOT NULL, desctext TEXT, voicepath TEXT, type INTEGER NOT NULL)";
	private static final String SQL_USER_ACCOUNT = "CREATE TABLE " + TableConstants.TABLE_USER_ACCOUNT + "(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, uid TEXT NOT NULL, accesstoken TEXT NOT NULL, expiredin LONG NOT NULL, username TEXT NOT NULL, accounttype INTEGER NOT NULL, avatar TEXT)";
	private static final String SQL_POST = "CREATE TABLE " + TableConstants.TABLE_POST + "(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, postid TEXT NOT NULL, imgid LONG NOT NULL, content TEXT, posttime LONG NOT NULL, commentcount INTEGER, lastupdate LONG NOT NULL)";
	private static final String SQL_POST_PLATFORM = "CREATE TABLE " + TableConstants.TABLE_POST_PLATFORM + "(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, postid TEXT NOT NULL, platformid TEXT NOT NULL, platformtype INTEGER NOT NULL)";
	private static final String SQL_COMMENT = "CREATE TABLE " + TableConstants.TABLE_COMMENT + "(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, comment_id TEXT NOT NULL, postid TEXT NOT NULL, content TEXT NOT NULL, commenttime LONG NOT NULL, commenttype INTEGER NOT NULL, author TEXT NOT NULL, avatar TEXT)";
	
	public DBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	
	/**
	 * 当数据库首次创建时执行该方法，一般将创建表等初始化操作放在该方法中执行.
	 * 重写onCreate方法，调用execSQL方法创建表
	 * */
	
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(SQL_FAVOURITE);
		db.execSQL(SQL_FAVOURITE_IMAGES);
		db.execSQL(SQL_IMAGE_DESC);
		db.execSQL(SQL_USER_ACCOUNT);
		db.execSQL(SQL_POST);
		db.execSQL(SQL_POST_PLATFORM);
		db.execSQL(SQL_COMMENT);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TableConstants.TABLE_FAVOURITE);
		db.execSQL("DROP TABLE IF EXISTS " + TableConstants.TABLE_FAVOURITE_IMAGES);
		db.execSQL("DROP TABLE IF EXISTS " + TableConstants.TABLE_IMAGE_DESC);
		db.execSQL("DROP TABLE IF EXISTS " + TableConstants.TABLE_USER_ACCOUNT);
		db.execSQL("DROP TABLE IF EXISTS " + TableConstants.TABLE_POST);
		db.execSQL("DROP TABLE IF EXISTS " + TableConstants.TABLE_POST_PLATFORM);
		db.execSQL("DROP TABLE IF EXISTS " + TableConstants.TABLE_COMMENT);
		onCreate(db);
	}
}
